package com.bis.ymyy.common.exception;


import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@ControllerAdvice(annotations = RestController.class)
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {

    //捕获异常
    @ExceptionHandler(value = Exception.class)
    public RestErrorResponse processExcetion(HttpServletRequest request, HttpServletResponse response, Exception e){
        if (e instanceof BusinessException) {
            log.info(e.getMessage());
            BusinessException be = (BusinessException) e;
            return new RestErrorResponse(be.getCode(), be.getMsg());
        } else {
        //非自定义异常类型，定义为99999系统未知错误
        log.error("系统异常：",e);
        return new RestErrorResponse(EnumCode.UNKNOWN.getCode(),e.getMessage());
        }
    }
}
